package com.cn.yugan.Activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Paint;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.cn.yugan.Cons.LoginConstants;
import com.cn.yugan.mobile.R;
import com.cn.yugan.Utils.AccountType;
import com.cn.yugan.Utils.Dialog.CustomDialog;


/**
 * Created by Rover on 2016/7/17 .
 * Email 357319517@qq.com
 * 注册成功-------->MainActivity(释放本活动)
 */
public class RegisterActivity extends AppCompatActivity implements View.OnClickListener {
    private Button bt_getcaptcha, bt_register;
    private EditText et_account, et_password, et_repassword, et_captcha, et_invitenum;
    private CheckBox cb_IsAccpect;
    private TextView tv_agreement;

    private String account = "";
    private int count = 0;
    Handler mhandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            bt_getcaptcha.setText(msg.obj.toString() + "秒");
            if ((int) msg.obj == 0) {
                bt_getcaptcha.setBackground(getResources().getDrawable(R.drawable.gray_btn_normal));
                bt_getcaptcha.setClickable(true);
                bt_getcaptcha.setText(getResources().getText(R.string.getcaptcha1));
            }
            super.handleMessage(msg);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        init();

    }

    public void init() {
        ActionBar actionBar = getSupportActionBar();
        actionBar.setTitle("注册");
        actionBar.setDisplayHomeAsUpEnabled(true);

        cb_IsAccpect = (CheckBox) findViewById(R.id.reg_cb_isaccpect);
        tv_agreement = (TextView) findViewById(R.id.reg_tv_agreement);
        tv_agreement.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
        tv_agreement.getPaint().setAntiAlias(true);
        tv_agreement.setOnClickListener(this);
        
        bt_getcaptcha = (Button) findViewById(R.id.reg_bt_getcaptcha);
        bt_register = (Button) findViewById(R.id.reg_bt_register);
        bt_getcaptcha.setOnClickListener(this);
        bt_register.setOnClickListener(this);

        et_account = (EditText) findViewById(R.id.reg_username);
        et_password = (EditText) findViewById(R.id.reg_password);
        et_captcha = (EditText) findViewById(R.id.reg_captcha);
        et_invitenum = (EditText) findViewById(R.id.reg_invitenum);
        et_repassword = (EditText) findViewById(R.id.reg_repassword);

        et_account.setImeOptions(EditorInfo.IME_ACTION_NEXT);
        et_password.setImeOptions(EditorInfo.IME_ACTION_NEXT);
        et_repassword.setImeOptions(EditorInfo.IME_ACTION_NEXT);
        et_captcha.setImeOptions(EditorInfo.IME_ACTION_NEXT);
        et_invitenum.setImeOptions(EditorInfo.IME_ACTION_NONE);

        et_account.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEND || (event != null && event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
                    et_password.requestFocus();
                    return true;
                }
                return false;
            }
        });

        et_password.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEND || (event != null && event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
                    et_repassword.requestFocus();
                    return true;
                }
                return false;
            }
        });
        et_repassword.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEND || (event != null && event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
                    et_captcha.requestFocus();
                    return true;
                }
                return false;
            }
        });

        et_captcha.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEND || (event != null && event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
                    et_invitenum.requestFocus();
                    return true;
                }
                return false;
            }
        });

        et_invitenum.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEND || (event != null && event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
                    return true;
                }
                return false;
            }
        });
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                this.finish();
                break;
        }
        return true;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.reg_bt_getcaptcha: {
                //获取验证码
                //暂缺验证服务器接口
                //待补充获取验证码逻辑
                account = et_account.getText().toString().trim();
                if (AccountType.IsEmail(account)) {
                    //邮箱逻辑
                    Toast.makeText(this, "邮箱", Toast.LENGTH_SHORT).show();
                    sendcaptcha();
                } else if (AccountType.IsPhone(account)) {
                    //手机逻辑
                    Toast.makeText(this, "手机", Toast.LENGTH_SHORT).show();
                    sendcaptcha();
                } else if (count == 0) {
                    count++;
                    Toast.makeText(this, "账号" +
                            "格式都不对啊，欧尼酱。下次鱼干娘就不提醒了咯。(●'◡'●)", Toast.LENGTH_SHORT).show();
                }
                break;
            }
            case R.id.reg_bt_register: {
                if (cb_IsAccpect.isChecked()) {
                    if (et_account.getText().toString().equals("") || et_password.getText().toString().equals("") || et_captcha.getText().toString().equals("")) {
                        Toast.makeText(this, "你好过分！信息都没有填完整就找我搭讪！！(╯▔皿▔)╯", Toast.LENGTH_SHORT).show();
                    } else if (et_password.getText().toString().equals(et_repassword.getText().toString())) {
                        //注册相关逻辑
                        //判断是否开放注册逻辑。
                        //1.手机号是否重复？密码是否合法？邀请码是否有效？验证码是否正确？
                        //以下为测试代码
                        Toast.makeText(RegisterActivity.this, "注册成功!", Toast.LENGTH_SHORT).show();
                        LoginAcitivity.instance.finish();
                        RegisterActivity.this.finish();
                        Intent intent = new Intent(RegisterActivity.this, IndexActivity.class);
                        intent.putExtra("LoginMode", LoginConstants.LOGIN_NORMAL);
                        startActivity(intent);
                    } else {
                        Toast.makeText(this, "两次输入的密码真的不一样(╯▔皿▔)╯", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    Toast.makeText(this, "协议都不接受还想和鱼干娘私定终生？", Toast.LENGTH_SHORT).show();
                }
                break;
            }
            case R.id.reg_tv_agreement:
                CustomDialog.Builder builder = new CustomDialog.Builder(this);
                builder.setWebview("file:///android_asset/agreement.html");
                builder.setTitle("用户须知");
                builder.setPositiveButton("接受", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        //设置你的操作事项
                        cb_IsAccpect.setChecked(true);
                    }
                });
                builder.setNegativeButton("取消",
                        new android.content.DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        });
                builder.create().show();
                break;
        }
    }

    //验证码的动画效果
    public void sendcaptcha() {
        bt_getcaptcha.setBackground(getResources().getDrawable(R.drawable.gray_btn_pressed));
        bt_getcaptcha.setClickable(false);
        new Thread(new Runnable() {
            @Override
            public void run() {
                int sec = 30;
                for (int i = 0; i < 31; i++) {
                    try {
                        Message msg = Message.obtain();
                        msg.obj = sec;
                        Thread.sleep(1000);
                        sec--;
                        mhandler.sendMessage(msg);
                    } catch (Exception e) {
                        Toast.makeText(RegisterActivity.this, "哎呀，鱼干娘被爆菊了！", Toast.LENGTH_LONG).show();
                    }
                }
            }
        }).start();
    }
}
